package org.itboys.mdl.controller.meal;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mdl.entity.mongo.meal.MdlMeal;
import org.itboys.mdl.service.mongo.meal.MdlMealService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/11/30
 * 描述：迈迪龙_套餐
 */
@Controller
@RequestMapping("/mdl/web/meal")
public class MdlMealController extends BaseController{
    @Autowired
    private MdlMealService mealService;
    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model){

        return "/maidilong/meal/index";
    }
    /**
     * 详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            Model model){

        MdlMeal meal = mealService.findOne(id);
        model.addAttribute("meal", meal);

        return "/maidilong/meal/input";
    }

    /**
     * 查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<MdlMeal> result=mealService.selectMeal(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 新增或者修改
     * @param request
     * @param response
     * @param meal
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute MdlMeal meal){
        try{
            mealService.saveMeal(meal);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/meal/save/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 删除
     * @param request
     * @param response
     * @param mealId
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="mealId",required=true) Long mealId){
        try {
            mealService.deleteMeal(mealId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = Finals.ERROMSG;
                logger.error("/mdl/web/meal/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
